package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品及其口味
     * 开启事务，保证数据一致性
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDishWithFlavors(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //向菜品表中插入1条数据
        dishMapper.insertDish(dish);
        //获取insert语句生成的主键值id
        Long dishId = dish.getId();
        //向口味表中插入数据，有可能有多条数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishId);
            }
            //SQL支持批量插入，所以这里可以一次性插入多条数据
            dishFlavorMapper.insertDishFlavors(dishFlavors);
        }
    }

    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //调用mapper查询数据
        Page<DishVO> dishPage = dishMapper.pageDish(dishPageQueryDTO);
        //将Page对象转换为PageResult对象
        PageResult pageResult = new PageResult();
        pageResult.setTotal(dishPage.getTotal());
        pageResult.setRecords(dishPage.getResult());
        return pageResult;
    }

    @Override
    public DishVO getDishById(Long id) {
        //调用mapper查询数据
        Dish dish = dishMapper.getDishById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        //查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.listDishFlavorsByDishId(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<Dish> listDishByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.listDishByCategoryId(categoryId);
        return dishes;
    }

    @Override
    public void updateDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //先删除原有的口味数据
        dishFlavorMapper.deleteDishFlavorByDishId(dish.getId());
        //再插入新的口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertDishFlavors(dishFlavors);
        }
        dishMapper.updateDish(dish);
    }

    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //判断菜品是否在启用状态，如果在启用状态，则不允许删除
        ids.forEach(id -> {
            Dish dish = dishMapper.getDishById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //判断菜品是否被套餐关联，如果被套餐关联，则不允许删除
        List<Long> setmealIds = setmealDishMapper.listSetmealIdByDishId(ids);
        if (setmealIds.size() > 0 && setmealIds!= null) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品及其口味数据
            dishMapper.deleteDish(ids);
            dishFlavorMapper.deleteDishFlavor(ids);
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
    }


}
